package cn.nlsde.controller;

import cn.nlsde.annotation.FreeAccess;
import cn.nlsde.common.SessionBean;
import cn.nlsde.constant.Constants;
import cn.nlsde.entity.User;
import cn.nlsde.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangwj on 16/2/27.
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    UserService userService;


    //    @ResponseBody
    @RequestMapping(value = "/test")
    public String test(HttpServletRequest request) {
        Map<String, Object> maps = new HashMap<>();
        logger.info("user = {}", request.getSession().getAttribute(Constants.SESSION_BEAN));
        return "test";
    }

    @ResponseBody
    @RequestMapping(value = "/testlogin")
    @FreeAccess
    public SessionBean testLogin(HttpServletRequest request) {
        Map<String, Object> maps = new HashMap<String, Object>();
        User user = new User();
        user.setUsername("张文杰");
        user.setPassword("test");
        SessionBean sb = new SessionBean();

        return sb;
    }


    @ResponseBody
    @RequestMapping(value = "/login")
    @FreeAccess
    public Map login(HttpSession session) {
        System.out.println("login test");
        Map<Integer, String> maps = new HashMap<Integer, String>();
        maps.put(1, "中国");
        maps.put(2, "login test");
        return maps;
    }

    @ResponseBody
    @RequestMapping("/get")
    @FreeAccess
//    @PrivilegeRequired(privileges = {PrivilegeBean.USER_ADD, PrivilegeBean.USER_ALTER})
    public ModelAndView get() {
        return new ModelAndView("login");
    }
}
